<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="searchForm" :inline="true">
            <el-form-item label="表单名称">
              <el-input v-model="searchForm.formName" placeholder="请输入表单名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="业务类型">
              <el-select v-model="searchForm.businessType" placeholder="请选择业务类型" clearable>
                <el-option v-for="item in form_template_type" :key="item.id" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">

          <el-col :span="1.5">
            <el-button type="primary" plain :icon="Plus" @click="handleAdd">新建表单模板</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getTableData(`reset`)"></right-toolbar>
        </el-row>
      </template>

      <el-table ref="roleTableRef" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="表单名称" prop="formName" :show-overflow-tooltip="true" />
        <el-table-column label="业务类型" prop="applyType" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ getApplyType(scope.row.applyType) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" :show-overflow-tooltip="true">
          <template #default="scope">
            <el-tag :type="scope.row.status == 'publish' ? 'success' : 'info'">{{ scope.row.status == "publish" ? '发布'
              : '未发布' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="updateTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" :icon="Edit" @click="handleAdd(scope.row)">
            </el-button>
            <el-button size="small" :icon="Delete" type="danger" @click="handleDelete(scope.row)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="common.total > 0" v-model:page="common.page" v-model:limit="common.size" :total="common.total"
        @pagination="getTableData" />

    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Delete, Plus, Edit } from '@element-plus/icons-vue'
import { getSysFormDocTemp ,deleteSysFormDocTemp} from '@/api/formTemplate/index'
const { proxy } = getCurrentInstance() as ComponentInternalInstance

const { form_template_type } = toRefs<any>(proxy?.useDict('form_template_type'))
const router = useRouter()

const loading = ref(false)
const showSearch = ref(true)
const dataList = ref([])
const selectionList = ref([])
const searchForm = reactive({
  formName: '',
  businessType: '',
})


const common = reactive({
  page: 1,
  size: 10,
  total: 0,
})

const getApplyType = (type) => {
  let item = form_template_type.value.find((item) => item.value == type)
  if (item) {
    return item.label
  }
}

/**
 * 查询模板列表
 */
const getTableData = (reset) => {
  loading.value = true
  if (reset == "reset") {
    common.size = 10
    common.page = 1
  }
  let params: any = {
    size: common.size,
    page: common.page - 1,
    sort: "id,desc",
  }
  if (searchForm.formName) {
    params.formName = searchForm.formName
  }
  if (searchForm.businessType) {
    params.applyType = searchForm.businessType
  }

  getSysFormDocTemp(params).then((res) => {
    dataList.value = res.data.rows
    common.total = res.data.total
    loading.value = false
  })
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  common.page = 1
  getTableData("reset")
}

/** 重置 */
const resetQuery = () => {
  searchForm.formName = ''
  searchForm.businessType = ''
  handleQuery()
}
const handleSelectionChange = (val) => {
  selectionList.value = val
}
/**删除按钮操作 */
const handleDelete = async (row) => {
  ElMessageBox.confirm('此操作将永久删除该表单, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const res = await deleteSysFormDocTemp([row.id])
    if (res.code == 200) {
      ElMessage.success('删除成功')
      // getList()
      getTableData("reset")
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleAdd = (item?) => {
  router.push({
    path: "/form-template",
    query: item
  })
}

onMounted(() => {
  // getList()
  getTableData("reset")
})
</script>
<style scoped lang="scss">
#form-template {
  padding: 20px 20px 45px;

  .form-list {
    .el-form {
      display: flex;
      flex-wrap: wrap;

      .search-btn {
        margin-left: 10px;

        :deep(.el-form-item__content) {
          margin-left: 0px !important;
        }
      }
    }

    .el-icon-delete {
      margin: 0px 10px;
    }

    .icon {
      cursor: pointer;
    }

    .el-pagination {
      margin-top: 18px;
    }
  }
}
</style>